//
//  ListView.m
//  Music
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ListView.h"

@implementation ListView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arr = [NSMutableArray  array];
        [self  dataHandel];
        [self  tableViewSubView];
        _downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return self;
}
-(void)dataHandel
{
    
    [[DataBaseHandle  shareDataBaseHandle]open];
    self.arr  =  [[DataBaseHandle  shareDataBaseHandle] selectCollect].mutableCopy;
    [[DataBaseHandle  shareDataBaseHandle]close];
}
-(void)tableViewSubView
{
    self.tableView = [[UITableView  alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight*0.6+49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   [self.tableView  registerClass:[CollectionCell  class] forCellReuseIdentifier:@"reuse"];
    self.tableView.rowHeight = 40;
    [self addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    CollectionCell  *cell = [tableView  dequeueReusableCellWithIdentifier:@"reuse"];
    self.collect = [self.arr  objectAtIndex:indexPath.row];
    cell.collect = self.collect;
   
    cell.title.text = self.collect.title;
    if (self.collect.urlid != nil) {
    cell.urlid.text = self.collect.urlid;
    }
   else{
       cell.urlid.text = nil;
    }
    return cell;
    
}
//进入收藏详情页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    [[DataBaseHandle  shareDataBaseHandle]open];//打开数据库
     Collection  *collect  = [self.arr  objectAtIndex:indexPath.row];
   
    [self.delegate passString:collect.title Singname:collect.urlid image:collect.image time:collect.time url:collect.url   arr:self.arr ];
    
    //[[DataBaseHandle  shareDataBaseHandle]close];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //[super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        
        Collection  *collect = [self.arr objectAtIndex:indexPath.row];
        [[DataBaseHandle  shareDataBaseHandle]open];//打开数据库
        
        [[DataBaseHandle  shareDataBaseHandle]deleteCollect:collect.title];//删除数据层
        [self.arr removeObjectAtIndex:indexPath.row];
        [self.tableView  deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];//视图层
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  *listView = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, 30)];
    listView.backgroundColor = [UIColor colorWithRed:0.76 green:0.82 blue:0.85 alpha:1.00];
    UILabel  *label = [[UILabel  alloc]initWithFrame:CGRectMake(Screenwidth/2-30, 5, 120, 20)];
    
    label.text = @"播放列表";
    label.font = [UIFont  systemFontOfSize:16];
    [listView  addSubview:label];
    
   
    return listView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView  *View = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, 30)];
    
    _downButton.frame = CGRectMake(0, 0, Screenwidth, 30);
    [_downButton  setTitle:@"关闭" forState:UIControlStateNormal];
    _downButton.titleLabel.font = [UIFont  systemFontOfSize:14];
    [_downButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_downButton addTarget:self action:@selector(downButton:) forControlEvents:UIControlEventTouchUpInside];
    [View  addSubview:_downButton];
    
    return View;
    
}
-(void)setArr:(NSMutableArray *)arr
{
    if (_arr != arr) {
        _arr = arr;
        [self.tableView  reloadData];
    }
    
}
-(void)downButton:(UIButton *)button
{
    [self.delegate  pass];
}
@end
